package com.ysd.lis.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ysd.common.Result;
import com.ysd.lis.entity.*;
import com.ysd.lis.mapper.PubTaskTempletMapper;
import com.ysd.lis.service.PubTaskColumnConnectService;
import com.ysd.lis.service.PubTaskTempletService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 样品可配置项分类表 服务实现类
 * </p>
 *
 * @author LiuQi
 * @since 2023-11-13
 */
@Service
public class PubTaskTempletServiceImpl extends ServiceImpl<PubTaskTempletMapper, PubTaskTemplet> implements PubTaskTempletService {
    @Resource
    private PubTaskTempletMapper pubTaskTempletMapper;
    @Resource
    private PubTaskColumnConnectService pubTaskColumnConnectService;
    @Resource
    private PublicTaskServiceImpl publicTaskService;
    @Override
    public Result findTaskTempletListByPageId(String pageId) {
        MPJLambdaWrapper<PubTaskTemplet> mpjLambdaWrapper = new MPJLambdaWrapper<>();
        mpjLambdaWrapper
                .distinct()
                .selectAll(PubTaskTemplet.class)
                .selectCollection(PubTaskColumnConnect.class, PubTaskTemplet::getTargetTableColumnData)
                .leftJoin(PubTaskColumnConnect.class, PubTaskColumnConnect::getTempleId, PubTaskTemplet::getId)
                .eq(PubTaskTemplet::getDelFlag, 0)
                .eq(PubTaskTemplet::getPageId, pageId);
        List<PubTaskTemplet> pubTaskTemplets = pubTaskTempletMapper.selectJoinList(PubTaskTemplet.class, mpjLambdaWrapper);
        return Result.succ(1, "查询成功", pubTaskTemplets);
    }

    @Override
    public Result getTempletTestList(String tableName, String templetId) {
        List<String> columnList = publicTaskService.getColumnList();
        List<Map<String, Object>> transmitTodoData = publicTaskService.getTransmitTodoData(tableName, columnList);
        List<Map<String, Object>> templetId1 = transmitTodoData.stream().filter(item1 -> templetId.equals(item1.get("TEMPLET_ID"))).collect(Collectors.toList());
        return Result.succ(1, "查询成功", templetId1);
    }

    /**
     * 通过模版编码查询所有的模版信息
     *
     * @param pageCode
     * @return
     */
    public List<PubTaskTemplet> findTaskTempleListByPageCode(String pageCode, String code) {
        MPJLambdaWrapper<PubTaskTemplet> mpjLambdaWrapper = new MPJLambdaWrapper<>();
        mpjLambdaWrapper
                .distinct()
                .selectAll(PubTaskTemplet.class)
                .selectCollection(PubTaskColumnConnect.class, PubTaskTemplet::getTargetTableColumnData)
                .leftJoin(PubTaskColumnConnect.class, PubTaskColumnConnect::getTempleId, PubTaskTemplet::getId)
                .eq(PubTaskTemplet::getDelFlag, 0)
                .eq(PubTaskTemplet::getPageCode, pageCode);
        if (StringUtils.isNotBlank(code)) {
            mpjLambdaWrapper.eq(PubTaskTemplet::getCode, code);
        }
        return pubTaskTempletMapper.selectJoinList(PubTaskTemplet.class, mpjLambdaWrapper);
    }

    @Transactional
    @Override
    public Result addTaskTemplet(PubTaskTemplet pubTaskTemplet) {
        boolean save = this.save(pubTaskTemplet);
        if (pubTaskTemplet.getTargetTableColumnData().size() > 0) {
            List<PubTaskColumnConnect> collect = pubTaskTemplet.getTargetTableColumnData().stream().map(item -> {
                PubTaskColumnConnect pubTaskColumnConnect = new PubTaskColumnConnect();
                BeanUtil.copyProperties(item, pubTaskColumnConnect);
                pubTaskColumnConnect.setTempleId(pubTaskTemplet.getId());
                return pubTaskColumnConnect;
            }).collect(Collectors.toList());
            pubTaskColumnConnectService.saveBatch(collect);
        }
        if (save) {
            return Result.succ(1, "添加模版成功", null);
        } else {
            return Result.fail("添加模版失败！");
        }
    }

    @Transactional
    @Override
    public Result editTaskTemplet(@RequestBody PubTaskTemplet pubTaskTemplet) {
        if (pubTaskTemplet.getTargetTableColumnData().size() > 0) {
            LambdaQueryWrapper<PubTaskColumnConnect> lambdaQueryWrapper = new LambdaQueryWrapper();
            lambdaQueryWrapper.eq(PubTaskColumnConnect::getTempleId, pubTaskTemplet.getId());
            pubTaskColumnConnectService.remove(lambdaQueryWrapper);
            List<PubTaskColumnConnect> collect = pubTaskTemplet.getTargetTableColumnData().stream().map(item -> {
                PubTaskColumnConnect pubTaskColumnConnect = new PubTaskColumnConnect();
                BeanUtil.copyProperties(item, pubTaskColumnConnect);
                pubTaskColumnConnect.setTempleId(pubTaskTemplet.getId());
                return pubTaskColumnConnect;
            }).collect(Collectors.toList());
            pubTaskColumnConnectService.saveBatch(collect);
        }
        boolean save = this.updateById(pubTaskTemplet);
        if (save) {
            return Result.succ(1, "编辑成功", null);
        } else {
            return Result.fail("编辑失败！");
        }
    }
    @Transactional
    @Override
    public Result deleteTaskTemplet(@RequestBody PubTaskTemplet pubTaskTemplet){
        // 删除字段对应关系表数据
        LambdaQueryWrapper<PubTaskColumnConnect> lambdaQueryWrapper = new LambdaQueryWrapper();
        lambdaQueryWrapper.eq(PubTaskColumnConnect::getTempleId, pubTaskTemplet.getId());
        pubTaskColumnConnectService.remove(lambdaQueryWrapper);
        LambdaUpdateWrapper<PubTaskTemplet> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper
                .set(PubTaskTemplet::getDelFlag, 1)
                .eq(PubTaskTemplet::getId, pubTaskTemplet.getId());
        int update = pubTaskTempletMapper.update(null, lambdaUpdateWrapper);
        if (update >= 0) {
            return Result.succ(1, "删除成功", null);
        } else {
            return Result.fail("删除失败！");
        }
    }
}
